import base from './base';
import vm from '@/main';

export default class user extends base {
    /**
* [login 后台登录]
*
* @return  {[type]}  [return description]
*/
    static userInfoSave (params) {
        let url = 'User/own';
        vm.saveLoading = true;
        this.post(url, params, true).then(data => {
            vm.$Message.success(data.msg);
            vm.$store.commit('logout', vm);
            vm.$router.push({
                name: 'login'
            });
        }).catch(error => {
            vm.$Message.error(error.message);
            console.error(error);
        });
        vm.saveLoading = false;
    }

    /**
* [getUserList 获取管理员列表]
*
* @return  {[type]}  [return description]
*/
    static getUserList (params) {
        let url = 'User/index';
        return this.get(url, params).then(data => {
            data.list.map(item => {
                item.loginTimes = item.userData.login_times ? item.userData.login_times : 0;
                item.lastLoginTime = item.userData.last_login_time;
                item.lastLoginIp = item.userData.last_login_ip;
            });
            return data;
        });
    }

    /**
* [delUser 删除管理员]
*
* @return  {[type]}  [return description]
*/
    static delUser (params) {
        let url = 'User/del';
        return this.get(url, params, true).then(data => {
            vm.$Message.success(data.msg);
        }).catch(error => {
            vm.$Message.error(error.message);
            console.error(error);
            throw error;
        });
    }

    /**
 * [changeStatus 变更管理员状态]
 *
 * @return  {[type]}  [return description]
 */
    static changeStatus (params) {
        let url = 'User/changeStatus';
        return this.get(url, params, true).then(data => {
            vm.$Message.success(data.msg);
        }).catch(error => {
            vm.$Message.error(error.message);
            console.error(error);
        });
    }

    /**
     * [getGroups 获取用户组列表]
     *
     * @return  {[type]}  [return description]
     */
    static getGroups (params) {
        let url = 'Auth/getGroups';
        return this.get(url, params).then(data => {
            // vm.$Message.success(data.msg);
            return data;
        }).catch(error => {
            vm.$Message.error(error.message);
            console.error(error);
        });
    }

    /**
     * [modifyUser 修改用户 包括 增加/编辑]
     *
     * @return  {[type]}  [return description]
     */
    static modifyUser (url, params) {
        return this.post(url, params, true).then(data => {
            vm.$Message.success(data.msg);
        }).catch(error => {
            vm.$Message.error(error.message);
            console.error(error);
        });
    }
}
